package com.admin.web;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.admin.model.FileMeta;
import com.admin.service.AdminCTSService;
import com.admin.service.AdminChooseService;
import com.admin.service.AdminOtherService;
import com.admin.service.AdminProjectService;
import com.admin.service.AdminSchoolService;
import com.admin.util.AdminProjectExcelTool;
import com.admin.util.AdminScoreExcelTool;
import com.admin.util.AdminStudentExcelTool;
import com.common.model.AdminUser;
import com.common.model.ClassInfo;
import com.common.model.Project;
import com.common.model.StudentUser;
import com.common.model.TimePlan;
import com.common.util.Md5Encrypt;

@Controller
@RequestMapping("/admin/admin")
public class AdminAdminWeb {
	@Autowired
	AdminSchoolService schooService;
	@Autowired
	AdminCTSService ctsService;
	@Autowired
	AdminChooseService choService;
	@Autowired
	AdminProjectService prjService;
	@Autowired
	AdminOtherService othSercice;
	/**
	 * 查询所有管理员列表
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showAdminList/{page}")
	public ModelAndView showAdminList(
			@PathVariable("page")int page,
			HttpSession session){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/other/showAdminList");
		AdminUser admin=(AdminUser)session.getAttribute("user");
		if(!admin.isSuperAdmin()){
			mav.setViewName("common/error");
			mav.addObject("error", "您没有权限,请勿非法操作");
			return mav;
		}
		mav.addObject("colleges", schooService.getAllCollege());
		int adminCount=othSercice.getAllAdminCount();
		int totalPage=(int)Math.ceil(adminCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("page", page);
		mav.addObject("adminList",othSercice.getAllAdmin(page));
		mav.addObject("adminCount",adminCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showAdminList");
		return mav;
	}
	/**
	 * 增加一个管理员
	 * @param admin 管理员信息
	 * @param session
	 * @return -1无权限 -2参数错误，-3重复no -4
	 */
	@ResponseBody
	@RequestMapping("/insertAdmin")
	public int insertNewAdmin(AdminUser admin,HttpSession session){
		AdminUser currentAdmin=(AdminUser)session.getAttribute("user");
		if(!currentAdmin.isSuperAdmin()) return -1;
		if(admin.getNo()==null||admin.getNo().trim().isEmpty()||admin.getName()==null||admin.getName().trim().isEmpty()){
			return -2;
		}
		if(admin.getGender()==null||admin.getPassword()==null||admin.getPassword().trim().isEmpty()||admin.getCollegeId()==null){
			return -2;
		}
		if(!admin.getGender().equals("男")){
			admin.setGender("女");
		}
		if(othSercice.getAdminByAdminNo(admin.getNo()).size()>0){
			return -3;
		}
		admin.setPassword(Md5Encrypt.encrypt(admin.getPassword().trim()));
		othSercice.insertNewAdmin(admin);
		return 0;
	}
	/**
	 * 删除一个管理员
	 * @param adminId
	 * @return -1无权限 -2删除自己
	 */
	@ResponseBody
	@RequestMapping("/deleteAdmin/{admin_id}")
	public int deleteAdmin(@PathVariable("admin_id")int adminId,HttpSession session){
		AdminUser currentAdmin=(AdminUser)session.getAttribute("user");
		if(!currentAdmin.isSuperAdmin()) return -1;
		if(currentAdmin.getId()==adminId){
			return -2;
		}
		othSercice.deleteAdmin(adminId);
		return 0;
	}
	/**
	 * 查询管理员
	 * @param adminId
	 * @return 
	 */
	@ResponseBody
	@RequestMapping("/getAdminByAdminId/{admin_id}")
	public AdminUser getAdminByAdminId(@PathVariable("admin_id")int adminId,HttpSession session){
		AdminUser currentAdmin=(AdminUser)session.getAttribute("user");
		if(!currentAdmin.isSuperAdmin()) return null;
		return othSercice.getAdminByAdminId(adminId);
	}
	/**
	 * 修改一个管理员
	 * @param adminId
	 * @return -1 无权限 -2参数不正确 -3重复no
	 */
	@ResponseBody
	@RequestMapping("/updateAdmin")
	public int updateAdmin(AdminUser admin,HttpSession session){
		AdminUser currentAdmin=(AdminUser)session.getAttribute("user");
		if(!currentAdmin.isSuperAdmin()) return -1;
		if(admin.getNo()==null||admin.getNo().trim().isEmpty()||admin.getName()==null||admin.getName().trim().isEmpty()){
			return -2;
		}
		if(admin.getGender()==null||admin.getCollegeId()==null||admin.getId()==null){
			return -2;
		}
		if(!admin.getGender().equals("男")){
			admin.setGender("女");
		}
		ArrayList<AdminUser> adminList= othSercice.getAdminByAdminNo(admin.getNo());
		if(adminList.size()>0&&adminList.get(0).getId()!=admin.getId()){
			return -3;
		}
		othSercice.updateAdmin(admin);
		return 0;
	}
	/**
	 * 重置密码
	 * @param id
	 * @param pass
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/resetPass")
	public int resetAdminPass(int id,String pass,HttpSession session){
		AdminUser currentAdmin=(AdminUser)session.getAttribute("user");
		if(!currentAdmin.isSuperAdmin()) return -1;
		othSercice.resetAdminPass(id, pass);
		return 0;
	}
	@RequestMapping("/showSystemSet")
	public ModelAndView showSystemSet(HttpSession session){
		ModelAndView mav=new ModelAndView();
		mav.addObject("colleges", schooService.getAllCollege());
		mav.setViewName("admin/other/showSystemSet");
		AdminUser admin=(AdminUser)session.getAttribute("user");
		if(!admin.isSuperAdmin()){
			mav.setViewName("common/error");
			mav.addObject("error", "您没有权限,请勿非法操作");
			return mav;
		}
		mav.addObject("currentMenu", "showSystemSet");
		return mav;
	}
	
	@ResponseBody
	@RequestMapping("/clearData")
	public int clearData(int type,String password,@RequestParam(required=false,defaultValue="-1")Integer collegeId,HttpSession session){
		AdminUser admin=(AdminUser)session.getAttribute("user");
		if(!admin.isSuperAdmin()){
			return -1;
		}
		if(!admin.getPassword().equalsIgnoreCase(Md5Encrypt.encrypt(password))){
			return -2;
		}
		if(type==1){
			othSercice.backAndDelete();
		}else if(type==2){
			othSercice.deleteBack();
		}else if(type==3){
			othSercice.deleteTeacher(collegeId);
		}
		return 0;
	}
	/**
	 * 查询所有归档
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showRecordList/{page}")
	public ModelAndView showRecordList(
			@PathVariable("page")int page,
			HttpSession session){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/other/showRecordList");
		int recordCount=othSercice.getRecordCount();
		int totalPage=(int)Math.ceil(recordCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("page", page);
		mav.addObject("recordList",othSercice.getRecord(page));
		mav.addObject("recordCount",recordCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showRecordList");
		return mav;
	}
}
